package com.start.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.start.reggie.dto.DishDto;
import com.start.reggie.entity.Dish;
import com.start.reggie.entity.DishFlavor;
import com.start.reggie.entity.SetmealDish;
import com.start.reggie.mapper.DishMapper;
import com.start.reggie.service.DishFlavorService;
import com.start.reggie.service.DishService;
import com.start.reggie.service.SetmealDishService;
import com.start.reggie.service.SetmealService;
import com.start.reggie.utils.CustomException;
import com.start.reggie.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper,Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Transactional //涉及多张表操作，需开启事务
    @Override
    public void saveWithFlavor(DishDto dishDto) {

        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        Long dishId = dishDto.getId();//菜品id

        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {

        //查询菜品基本信息，从dish表中查询
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //查询当前菜品对应的口味信息，从dish_flavor表中查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);

        return dishDto;

    }

    /**
     * 更新菜品，同时更新对应的口味数据
     * @param dishDto
     */
    @Transactional
    @Override
    public void updateWithFlavor(DishDto dishDto) {

        //更新dish表基本信息
        this.updateById(dishDto);

        //清理当前菜品对应的口味数据——dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        //添加当前提交过来的口味数据——dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        //菜品口味
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 批量或单独删除菜品
     * @param ids
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {

        //构造条件查询
        LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(ids != null,Dish::getId,ids);

        List<Dish> list = this.list(queryWrapper1);

        for (Dish dish : list) {

            //判断删除的菜品的售卖状态
            Integer status = dish.getStatus();

            if (status == 0){
                //删除菜品
                this.removeById(dish.getId());
                //清理当前菜品对应的口味数据——dish_flavor表的delete操作
                LambdaQueryWrapper<DishFlavor> queryWrapper2 = new LambdaQueryWrapper<>();
                queryWrapper2.eq(DishFlavor::getDishId,dish.getId());
                dishFlavorService.remove(queryWrapper2);
            }else {
                //回滚事务
                throw new CustomException("删除的菜品中含有正在售卖的菜品，无法全部删除");
            }

        }

    }

    /**
     * 批量或单独更新菜品状态
     * @param status
     * @param ids
     */
    @Transactional
    @Override
    public void updateByIds(Integer status,List<Long> ids) {

        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ids != null,Dish::getId,ids);
        List<Dish> list = this.list(queryWrapper);
        for (Dish dish : list){

            if (status == 0){
                //统计结果如果不等于0/不为null，则代表在套餐中包含有选中菜品，此时不能更新
                LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
                setmealDishLambdaQueryWrapper.eq(SetmealDish::getDishId, dish.getId());
                //int count = setmealDishService.count(setmealDishLambdaQueryWrapper);
                //if (count == 0){
                SetmealDish setmealDish = setmealDishService.getOne(setmealDishLambdaQueryWrapper);
                if (setmealDish == null){
                    dish.setStatus(status);
                    this.updateById(dish);
                }else {
                    throw new CustomException("更新状态的菜品在套餐中被包含，无法全部更新");
                }
            }else {
                dish.setStatus(status);
                this.updateById(dish);
            }

        }

    }

}
